package main

import "fmt"

/*
*
函数选项模式
*/

type DbOptions struct {
	Host string
	Port int
	User string
	Pass string
}

type Option func(*DbOptions)

func WithHost(host string) Option {
	return func(o *DbOptions) {
		o.Host = host
	}
}

func WithPort(port int) Option {
	return func(o *DbOptions) {
		o.Port = port
	}
}

func NewOptions(opt ...Option) DbOptions {
	option := &DbOptions{
		Host: "127.0.0.1",
		Port: 3306,
		User: "ye",
		Pass: "123456",
	}
	for _, o := range opt {
		o(option)
	}
	return *option
}

func main() {
	options := NewOptions(WithHost("192.168.0.1"), WithPort(8080))
	fmt.Println(options)
}
